package tests;

import static org.junit.Assert.*;

import java.sql.Connection;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import veilingDomein.Blocked;
import veilingDomein.Krediet;
import veilingDomein.User;
import veilingService.DBController;
import veilingService.IUserService;
import veilingService.ServiceProvider;

public class UserServiceTests {

	// Tests voor de UserService methodes
	
	@Before 
	public void setup() {
		
		DBController db = new DBController();
		Connection con = db.connect();

	}

	@Test
	public void testRegistratie() {

		IUserService ius = ServiceProvider.getUserService();
		try {
			User user = new User("John doe", "Teststraat", "TestLand", "John", "John12345",
					"john@hotmail.com", 1, 1);
			ius.createUser(user);
		} catch (Exception e1) {
			Assert.fail();
			e1.printStackTrace();
		}
	}
	@Test
	public void testGeenWachtwoord() {

		IUserService ius = ServiceProvider.getUserService();
		try {
			User user = new User("John doe", "Testland", "TestLand", "John", "fout",
					"john@hotmail.com", 1, 1);
			ius.createUser(user);
			Assert.fail();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	@Test
	public void testvoegKredietToe() {
		
		IUserService ius = ServiceProvider.getUserService();
		try {
			User algemeen = new User("JUnit", "Teststraat", "TestLand", "Junit test", "Junit12345",
					"junit@hotmail.com", 1, 2);

			ius.createUser(algemeen);
			ius.voegKredietToe(algemeen, 10);
			System.out.println("1. TestvoegKredietToe: Het aantal krediet van "+ algemeen.getUsername() + " is "  + algemeen.getKrediet());

		}catch(Exception e){
			Assert.fail();
			e.printStackTrace();
		}
	}
	@Test
	public void testUpdateKredit() {
		
		IUserService ius = ServiceProvider.getUserService();
		try {
			User algemeen = new User("JUnit", "Teststraat", "TestLand", "Junit test", "Junit12345",
					"junit@hotmail.com", 1, 2);
			ius.createUser(algemeen);
			ius.voegKredietToe(algemeen, 10);
			System.out.println("Test2: Voor de update is het aantal krediets" +" " + algemeen.getKrediet());
			
			Krediet k = new Krediet();   // Nieuw krediet object
			k = algemeen.getKrediet();
			k.stort(200);
			algemeen.setKrediet(k);
			System.out.println("Test2: Na Update is get aantal krediets" + " " +  algemeen.getKrediet());
			
		}catch (Exception e) {
			Assert.fail();
			e.printStackTrace();
		}
	}
	@Test
	public void testBlokkeerUser() {
		
		IUserService ius = ServiceProvider.getUserService();
		User algemeen;
		
		try {
			algemeen = new User("JUnit", "Teststraat", "TestLand", "Junit test", "Junit12345",
					"junit@hotmail.com", 1, 2);
			
			ius.createUser(algemeen);
			User user = ius.getUser(algemeen.getUsername());
			ius.BlokkeerUser(user, "01/12/2012", "GoedeJunit test");
			
			
			System.out.println("" + ius.isBlocked(algemeen.getUsername()));
			
		} catch (Exception e) {
			
			Assert.fail();
			e.printStackTrace();
		}
	}
}
